#include<bits/stdc++.h>
#define ll long long
using namespace std;

int _,n,m,x[105],y[105],v[105];
ll ans;
bool pri[60005];

inline bool spi(int w){return !pri[w]&&(w%20==3||w<=10);}

inline int dis(int a,int b){return abs(x[a]-x[b])+abs(y[a]-y[b]);}

inline void sol()
{
    cin>>n>>m;

    ans=0;
    for(int i=1;i<=n;i++)
        cin>>x[i]>>y[i];

    for(int i=1;i<=n;i++)
        cin>>v[i];

    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            for(int k=j+1;k<=n;k++)
            {
                int a=dis(i,j),b=dis(i,k),c=dis(j,k);
                int d=a+b+c-max(max(a,b),c)-min(min(a,b),c);
                if(spi(d))
                    ans+=v[i]+v[j]+v[k];
            }
        }
    }

    cout<<ans<<'\n';
}

int main()
{
    freopen("chess.in","r",stdin);
    freopen("chess.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);

    pri[1]=1;
    for(int i=2;i<=60000;i++)
    {
        if(pri[i])
            continue;
        for(int j=i+i;j<=60000;j+=i)
            pri[j]=1;
    }
    
    cin>>_;
    while(_--)
        sol();

    return 0;
}